<?php

class Order_Resource_Item{
	
	protected $_cartModel;
	
	protected $_orderItemModel;
	
	protected $_objectItemModel;
	
	protected $orderId;
	
	public function init(){
		$this->_cartModel = new Buyback_Model_Cart();
	}
	
	public function __construct($orderId){
		$this->init();
		$this->orderId = $orderId;
	}
	
	public function saveOrderItems(){
		$this->_cartModel->rewind();
		while($this->_cartModel->valid()){
			if($this->_cartModel->current() instanceOf Buyback_Resource_Cart_ValidItem){
				
				$class = 'Doctrine_Model_' . $this->_cartModel->current()->ItemType . 's';
				$this->_objectItemModel = new $class();
				$this->_objectItemModel->Isbn10 = $this->_cartModel->current()->ItemTypeObject->Isbn10;
				$this->_objectItemModel->Isbn13 = $this->_cartModel->current()->ItemTypeObject->Isbn13;
				$this->_objectItemModel->title = $this->_cartModel->current()->ItemTypeObject->Title;
				$this->_objectItemModel->author = $this->_cartModel->current()->ItemTypeObject->Authors;
				if(is_array($this->_cartModel->current()->ItemTypeObject->Authors))
					$this->_objectItemModel->author = implode(',', $this->_cartModel->current()->ItemTypeObject->Authors);
				else
					$this->_objectItemModel->author = $this->_cartModel->current()->ItemTypeObject->Authors;
				$this->_objectItemModel->publisher = $this->_cartModel->current()->ItemTypeObject->Publisher;
				$this->_objectItemModel->publishYear = $this->_cartModel->current()->ItemTypeObject->PublishYear;
				$this->_objectItemModel->image = $this->_cartModel->current()->ItemTypeObject->Image;
				$this->_objectItemModel->edition = $this->_cartModel->current()->ItemTypeObject->Edition;
				$this->_objectItemModel->height = $this->_cartModel->current()->ItemTypeObject->Dimensions->Height;
				$this->_objectItemModel->width = $this->_cartModel->current()->ItemTypeObject->Dimensions->Width;
				$this->_objectItemModel->length = $this->_cartModel->current()->ItemTypeObject->Dimensions->Length;
				$this->_objectItemModel->save();
				
				for($i = 0; $i < $this->_cartModel->current()->Quantity; $i++){
					$this->_orderItemModel = new Doctrine_Model_OrderItems();
					$this->_orderItemModel->orderId = $this->orderId;
					$this->_orderItemModel->itemType = $this->_cartModel->current()->ItemType;
					$this->_orderItemModel->itemTypeId = $this->_objectItemModel->id;
					$this->_orderItemModel->weight = $this->_cartModel->current()->ItemTypeObject->Dimensions->Weight;
					$this->_orderItemModel->priceMetrics = $this->_cartModel->current()->ItemTypeObject->Metric;
					$this->_orderItemModel->price = $this->_cartModel->current()->Offers->OfferPrice;
					$this->_orderItemModel->priceSource = $this->_cartModel->current()->ItemTypeObject->Source;
					$this->_orderItemModel->maxQnty = $this->_cartModel->current()->MaxQnty;
					$this->_orderItemModel->height = $this->_cartModel->current()->ItemTypeObject->Dimensions->Height;
					$this->_orderItemModel->width = $this->_cartModel->current()->ItemTypeObject->Dimensions->Width;
					$this->_orderItemModel->length = $this->_cartModel->current()->ItemTypeObject->Dimensions->Length;
				
					$this->_orderItemModel->save();
				}
			}	
			$this->_cartModel->next();
		}
		
		$this->_cartModel->removeItem();
	}
}